﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using DieuTriNoiTru.includes;
using System.Data.SqlClient;
using DieuTriNoiTru.includes;


namespace DieuTriNoiTru.includes
{
    class MTSTable
    {
        // ten bang trong csdl
        public string Tablename { get; set; }
        // chuoi du lieu muon lay
        public string GetData { get; set; }
        // khoa chinh trong truong hop sua va xoa
        public string Key { get; set; }
        // chuoi du lieu them vao bang
        public string Data { get; set; }
        // chuoi ten cac truong cua bang muon them du lieu
        public string Dataname { get; set; }
        // chuoi du lieu sua
        public string Updatedata { get; set; }

        public connection Obj = new connection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dtb = new DataTable();
        DataSet d = new DataSet();

        public DataTable Select()
        {
            try
            {
                this.Obj.Open();
                dtb.Clear();
                string sql = "select " + this.GetData + " from " + this.Tablename;
                cmd.Connection = Obj.conn;
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                da.SelectCommand = cmd;
                da.Fill(dtb);
                this.Obj.Close();
                return dtb;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return null;
            }
        }
        public void Insert()
        {
            try
            {
                this.Obj.Open();
                dtb.Clear();
                string sql = "insert into " + this.Tablename + "(" + this.Dataname + ") values(" + this.Data + ")";
                //cmd.Connection = Obj.conn;
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                this.Obj.Close();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }
        public void Delete()
        {
            try
            {
                this.Obj.Open();
                dtb.Clear();
                string sql = "delete from " + this.Tablename + " where " + this.Key;
                cmd.Connection = Obj.conn;
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                this.Obj.Close();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }
        public void Update()
        {
            try
            {
                this.Obj.Open();
                dtb.Clear();
                string sql = "update " + this.Tablename + " set " + this.Updatedata + " where " + this.Key;
                //cmd.Connection = Obj.conn;
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                this.Obj.Close();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }
    }
}
